<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="1200" destroy-on-close @closed="$emit('closed')" align-center :close-on-click-modal="false">
        <el-container>
            <el-main>
                <el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="110px" label-position="right" style="height:450px;overflow:auto">
                    <el-row :gutter="1" >
                        <el-col :span="8">
                            <el-form-item label="订单号" prop="order_no">
                                <el-input v-model="form.order_no" placeholder="请输入订单号" clearable autocomplete="off"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="下单日期" prop="plan_date">
                                <el-date-picker v-model="form.plan_date" :value-format="'YYYY-MM-DD'" style="width:100%" type="date" placeholder="请选择下单日期" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="备注" prop="remark">
                                <el-input v-model="form.remark" placeholder="请输入" clearable autocomplete="off"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row :gutter="1">
                        <el-button type="primary" size="small" icon="el-icon-plus" @click="openWu">选择物料</el-button>
                        <sc-form-table ref="table" v-model="form.material_data" :addTemplate="addTemplate" placeholder="暂无数据" hideAdd>
                            <el-table-column prop="material_name" label="物料名称">
                                <template #default="scope">
                                    <div>{{scope.row.material_name}}</div>
                                </template>
                            </el-table-column>
                            <el-table-column prop="material_spec_code" label="物料编码" align="center">
                                <template #default="scope">
                                    <div>{{scope.row.material_spec_code}}</div>
                                </template>
                            </el-table-column>
                            <el-table-column prop="need_num" label="数量" align="center">
                                <template #default="scope">
                                    <el-input v-model="scope.row.need_num" placeholder="请输入数量"></el-input>
                                </template>
                            </el-table-column>
                            <el-table-column prop="source" label="采购类型" align="center">
                                <template #default="scope">
                                    <el-select v-model="scope.row.source"  filterable style="width: 100%" placeholder="请选择">
                                        <el-option label="大货" :value="0"/>
                                        <el-option label="版料" :value="2"/>
                                    </el-select>
                                </template>
                            </el-table-column>
                            <el-table-column prop="purchase_unit" label="采购单位" align="center">
                                <template #default="scope">
                                    <el-select v-model="scope.row.purchase_unit"  filterable style="width: 100%" placeholder="请选择" disabled>
                                        <el-option v-for="item in unitList" :key="item.id" :label="item.unit_name" :value="item.id"/>
                                    </el-select>
                                </template>
                            </el-table-column>
                            <el-table-column prop="remark" label="备注" align="center">
                                <template #default="scope">
                                    <el-input v-model="scope.row.remark" placeholder="请输入内容"></el-input>
                                </template>
                            </el-table-column>
                        </sc-form-table>
                    </el-row>
                </el-form>
            </el-main>
            <el-footer>
                <div style="display:flex;justify-content: end;">
                    <el-button @click="visible=false" >取 消</el-button>
			        <el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">确 定</el-button>
                </div>
            </el-footer>
        </el-container>
	</el-dialog>
    <wu v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false" draggable></wu>
</template>

<script>
import wu from './wu.vue';
	export default {
		components: {wu},
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '添加购物车',
					edit: '编辑购物车',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
                dialog: {
                    save: false
                },
				//表单数据
				form: {
                    order_no:"",
					plan_date: new Date().toISOString().slice(0, 10),
                    remark:"",
                    material_data:[],
				},
                addTemplate: {
					material_id:"",
                    material_spec_code:"",
                    need_num:"",
                    cate_id:"",
                    remark:"",
				},
				//验证规则
				rules: {
					plan_date: [
						{required: true, message: '请选择下单日期'}
					],
				},
                unitList:[],   //单位
                materName:"",
			}
		},
		mounted() {
            this.getUnit()
		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
            // 打开物料弹窗
            openWu(row,index){
                this.dialog.save = true
                this.$nextTick(() => {
                    this.$refs.saveDialog.open('add').setData({index:index})
                })
            },
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						var res  = [];
						res = await this.$API.material.getshopCar.add.post(this.form);
						this.isSaveing = false;
						if(res.code == 1){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}else{
						return false;
					}
				})
			},
			//表单注入数据
			async setData(data){
                this.form.id = data.id
			},
            // 获取单位
            async getUnit(){
                var res = await this.$API.all.unit.get();
                this.unitList = res.data;
            },
            handleSuccess(data) {
                let a = []
                data.map((item)=>{
                    a.push({
                        material_id:item.material.material_id,
                        material_spec_code:item.spec_code,
                        material_name:item.material.material_name,
                        cate_id:item.material.category_id,
                        purchase_unit:Number(item.material.unit)
                    })
                })
                let existingIds = this.form.material_data.map(item => item.material_spec_code);
                for(let i = a.length - 1; i >= 0; i--){
                    if(existingIds.includes(a[i].material_spec_code)){
                        a.splice(i, 1);
                    }
                }
                this.form.material_data = [...this.form.material_data,...a]
            },
		}
	}
</script>

<style lang="scss">
.el-form-item--default{
    align-items: center;
}
</style>
